package com.ytjj.qmyx.mall.controller.h5;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.OrderUtils;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.model.dto.ShoppingCardDto;
import com.ytjj.qmyx.mall.model.response.ActivityProductResponse;
import com.ytjj.qmyx.mall.model.response.ShoppingCardIndexResponse;
import com.ytjj.qmyx.mall.service.ActivityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

/**
 * 购物卡活动
 *
 **/
@RestController
@RequestMapping("/h5/shoppingcard")
public class H5ShoppingCardController extends BaseController {
    @Resource
    private ActivityService activityService;

    @Resource
    private RedisService redisService;

    @GetMapping("index")
    public CommonResult index () {
        ShoppingCardIndexResponse indexResponse=new ShoppingCardIndexResponse();
        String redisKey = RedisConstants.SHOPPING_CARD_H5 + TokenUtil.getToken();
        String cardDto = redisService.get(redisKey);
        ShoppingCardDto shoppingCardDto=null;
        if (StringUtils.isNotEmpty(cardDto)) {
            shoppingCardDto=JSONObject.parseObject(cardDto, ShoppingCardDto.class);
        }else{
            Calendar calendar=Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH,3);
            shoppingCardDto=new ShoppingCardDto();
            shoppingCardDto.setAmount(new BigDecimal(100));
            shoppingCardDto.setCardNo(OrderUtils.getShoppingCard());
            shoppingCardDto.setPeriodOfValidity(new SimpleDateFormat("yyyy.MM.dd").format(calendar.getTime()));

            redisService.set(redisKey, JSONObject.toJSONString(shoppingCardDto));
            redisService.expire(redisKey, 60*60*24*3);
        }
        indexResponse.setAmount(shoppingCardDto.getAmount());
        indexResponse.setCardNo(shoppingCardDto.getCardNo());
        indexResponse.setPeriodOfValidity(shoppingCardDto.getPeriodOfValidity());
        indexResponse.setFirstProductList(activityService.getActivityList(1, 10,ActivityTypeEnums.SHOPPING_CARD.name(),69));
        List<ActivityProductResponse> productList = activityService.getActivityList(1, 10,ActivityTypeEnums.SHOPPING_CARD.name(),70);
        Collections.shuffle(productList);
        indexResponse.setSecondProductList(productList);
        return CommonResult.success(indexResponse);
    }

    /**
     * 栏目商品列表
     *
     * @return
     */
    @GetMapping("/queryListByPage")
    public CommonResult queryListByPage(@RequestParam(value = "columnId", required = false, defaultValue = "0") Integer columnId,
                                                 @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "flag", required = false, defaultValue = "0") Integer flag,
                                        @RequestParam(value = "jptype", required = false, defaultValue = "0") Integer jptype) {
        if(1==jptype){
            columnId=-1;
        }
        List<ActivityProductResponse> productList = activityService.getActivityList(pageNum, pageSize,ActivityTypeEnums.SHOPPING_CARD.name(),columnId);
        if(1==flag){//打乱顺序
            Collections.shuffle(productList);
        }
        return CommonResult.success(productList);
    }
}
